//
//  JGMainViewController.m
//  JGAdRolling
//
//  Created by JJetGu on 15/7/13.
//  Copyright (c) 2015年 JJetGu. All rights reserved.
//

#import "JGMainViewController.h"
#import "JGContents.h"
#import "JGContentsCell.h"
#import "MJExtension.h"

#define JGCellIdentifier @"contents"
#define JGMaxSections 100

@interface JGMainViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageContorol;

@property (nonatomic, strong) NSArray *contentArray;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation JGMainViewController

-(NSArray *)contentArray
{
    if (!_contentArray) {
        self.contentArray = [JGContents objectArrayWithFilename:@"contents.plist"];
        self.pageContorol.numberOfPages = self.contentArray.count;
    }
    return _contentArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册CollectionViewCell
    [self.collectionView registerNib:[UINib nibWithNibName:@"JGContentsCell" bundle:nil] forCellWithReuseIdentifier:JGCellIdentifier];
    
    //添加定时器
    [self addTimer];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 马上显示回最中间那组的数据
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:JGMaxSections/2] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}

/**
 *  添加定时器
 */
-(void)addTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

/**
 *  移除定时器
 */
-(void)removeTimer
{
    // 停止定时器
    [self.timer invalidate];
    self.timer = nil;
}

- (NSIndexPath *)resetIndexPath
{
    // 当前正在展示的位置
    NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
    // 显示回最中间那组的数据
    NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:JGMaxSections/2];
    [self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    return currentIndexPathReset;
}

-(void)nextPage
{
    // 1.马上显示回最中间那组的数据
    NSIndexPath *currentIndexPathReset = [self resetIndexPath];
    
    // 2.计算出下一个需要展示的位置
    NSInteger nextItem = currentIndexPathReset.item + 1;
    NSInteger nextSection = currentIndexPathReset.section;
    if (nextItem == self.contentArray.count) {
        nextItem = 0;
        nextSection++;
    }
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    
    // 3.通过动画滚动到下一个位置
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.contentArray.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return JGMaxSections;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JGContentsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JGCellIdentifier forIndexPath:indexPath];
    
    cell.contents = self.contentArray[indexPath.item];
    
    return cell;
}

#pragma mark  - UICollectionViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % self.contentArray.count;
    self.pageContorol.currentPage = page;
}


@end
